import dynamic from 'next/dynamic'; import NextImage from 'next/image'; import { useEffect, useState } from 'react'; import { useRouter } from 'next/router'; import Seo from '../../../core/components/Seo.jsx'; import Promocrumb from '../../../lib/promo/components/Promocrumb.jsx'; import LogoSpinner from '../../../core/components/elements/Spinner/LogoSpinner.jsx'; import ProductPromoCard from '../../../../src-migrate/modules/product-promo/components/Card.tsx'; import React from 'react'; import DesktopView from '../../../core/components/views/DesktopView.jsx'; import MobileView from '../../../core/components/views/MobileView.jsx'; import 'swiper/swiper-bundle.css'; import useDevice from '../../../core/hooks/useDevice.js'; import ProductFilterDesktop from '../../../lib/product/components/ProductFilterDesktopPromotion.jsx'; import ProductFilter from '../../../lib/product/components/ProductFilter.jsx'; import { HStack, Image, Tag, TagCloseButton, TagLabel } from '@chakra-ui/react'; import { formatCurrency } from '../../../core/utils/formatValue.js'; import Pagination from '../../../core/components/elements/Pagination/Pagination.js'; import whatsappUrl from '../../../core/utils/whatsappUrl.js'; import _ from 'lodash'; import useActive from '../../../core/hooks/useActive.js'; import useProductSearch from '../../../lib/promo/hooks/usePromotionSearch.js'; const BasicLayout = dynamic(() => import('../../../core/components/layouts/BasicLayout.jsx') ); export default function PromoDetail() { const router = useRouter(); const { slug = '', brand = '', category = '', page = '1' } = router.query; const [currentPage, setCurrentPage] = useState(parseInt(10) || 1); const [orderBy, setOrderBy] = useState(router.query?.orderBy); const popup = useActive(); const prefixUrl = `/shop/promo/${slug}`; const [queryFinal, setQueryFinal] = useState({}); const [limit, setLimit] = useState(30); const [q, setQ] = useState('*'); const [finalQuery, setFinalQuery] = useState({}); const [products, setProducts] = useState(null); const [brandValues, setBrand] = useState( !router.pathname.includes('brands') ? router.query.brand ? router.query.brand.split(',') : [] : [] ); const [categoryValues, setCategory] = useState( router.query?.category?.split(',') || router.query?.category?.split(',') ); const [priceFrom, setPriceFrom] = useState(router.query?.priceFrom || null); const [priceTo, setPriceTo] = useState(router.query?.priceTo || null); useEffect(() => { const newQuery = { fq: `type_value_s:${slug}`, page: router.query.page ? router.query.page : 1, brand: router.query.brand ? router.query.brand : '', category: router.query.category ? router.query.category : '', priceFrom: router.query.priceFrom ? router.query.priceFrom : '', priceTo: router.query.priceTo ? router.query.priceTo : '', limit: router.query.limit ? router.query.limit : '', orderBy: router.query.orderBy ? router.query.orderBy : '', }; setFinalQuery(newQuery); }, [ router.query, prefixUrl, slug, brand, category, priceFrom, priceTo, currentPage, ]); useEffect(() => { setQueryFinal({ ...finalQuery, q, limit, orderBy }); }, [ router.query, prefixUrl, slug, brand, category, priceFrom, priceTo, currentPage, finalQuery, ]); const { productSearch } = useProductSearch({ query: queryFinal, operation: 'OR', }); const pageCount = Math.ceil(productSearch.data?.response.numFound / limit); const productStart = productSearch.data?.responseHeader.params.start; const productRows = limit; const productFound = productSearch.data?.response.numFound; useEffect(() => { setProducts(productSearch.data?.response?.products); }, [productSearch]); const brands = []; for ( let i = 0; i < productSearch.data?.facet_counts?.facet_fields?.manufacture_name_s.length; i += 2 ) { const brand = productSearch.data?.facet_counts?.facet_fields?.manufacture_name_s[i]; const qty = productSearch.data?.facet_counts?.facet_fields?.manufacture_name_s[i + 1]; if (qty > 0) { brands.push({ brand, qty }); } } const categories = []; for ( let i = 0; i < productSearch.data?.facet_counts?.facet_fields?.category_name.length; i += 2 ) { const name = productSearch.data?.facet_counts?.facet_fields?.category_name[i]; const qty = productSearch.data?.facet_counts?.facet_fields?.category_name[i + 1]; if (qty > 0) { categories.push({ name, qty }); } } function capitalizeFirstLetter(string) { string = string.replace(/_/g, ' '); return string.replace(/(^\w|\s\w)/g, function (match) { return match.toUpperCase(); }); } const handleDeleteFilter = async (source, value) => { let params = { q: router.query.q, orderBy: '', brand: brandValues.join(','), category: categoryValues?.join(','), priceFrom: priceFrom || '', priceTo: priceTo || '', }; let brands = brandValues; let catagories = categoryValues; switch (source) { case 'brands': brands = brandValues.filter((item) => item !== value); params.brand = brands.join(','); await setBrandValues(brands); break; case 'category': catagories = categoryValues.filter((item) => item !== value); params.category = catagories.join(','); await setCategoryValues(catagories); break; case 'price': params.priceFrom = ''; params.priceTo = ''; break; case 'delete': params = { q: router.query.q, orderBy: '', brand: '', category: '', priceFrom: '', priceTo: '', }; break; } handleSubmitFilter(params); }; const handleSubmitFilter = (params) => { params = _.pickBy(params, _.identity); params = toQuery(params); router.push(`${slug}?${params}`); }; const toQuery = (obj) => { const str = Object.keys(obj) .map( (key) => `${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}` ) .join('&'); return str; }; const whatPromo = capitalizeFirstLetter(slug); const queryWithoutSlug = _.omit(router.query, ['slug']); return (

Promo {whatPromo}

{products?.length >= 1 && (
)} {productSearch.isLoading && (
)} {products && ( <>
{products?.map((promotion) => (
))}
)}

Promo {whatPromo}

{productSearch.isLoading ? (
) : products && products.length >= 1 ? ( <>
{products?.map((promotion) => (
))}
) : (

Belum ada promo pada kategori ini

)}
Barang yang anda cari tidak ada?{' '} Hubungi Kami
); } const FilterChoicesComponent = ({ brandValues, categoryValues, priceFrom, priceTo, handleDeleteFilter, }) => (
{brandValues?.map((value, index) => ( {value} handleDeleteFilter('brands', value)} /> ))} {categoryValues?.map((value, index) => ( {value} handleDeleteFilter('category', value)} /> ))} {priceFrom && priceTo && ( {formatCurrency(priceFrom) + '-' + formatCurrency(priceTo)} handleDeleteFilter('price', priceFrom)} /> )} {brandValues?.length > 0 || categoryValues?.length > 0 || priceFrom || priceTo ? ( ) : ( '' )}
);